package com.example.lovedogs.zizhen;

import java.lang.reflect.Type;
import java.util.List;

import com.example.lovedogs.OtherActivity;
import com.example.lovedogs.R;
import com.example.lovedogs.R.id;
import com.example.lovedogs.R.layout;
import com.example.lovedogs.R.string;
import com.example.lovedogs.domain.SickInfoDisease;
import com.example.lovedogs.ui.ExpandableTextView;
import com.example.lovedogs.util.SpanUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SickInfoDiseaseDetailsActivity extends Activity {
//	private String url="http://192.168.191.2:8080/LoveDogs04/JsonSickInfoDiseaseDetails";
	private String url;
	private TextView tv_name;
	private TextView tv_symptom;
	private ExpandableTextView tv_definition;
	private ExpandableTextView tv_cause;
	private ExpandableTextView tv_cure;
	private int keyNo;
	private SickInfoDisease data;
	private String keySymptom;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sickinfodisease_details);
		url=getString(R.string.serverurl)+"JsonSickInfoDiseaseDetails";
		titlebar();
		initTv();
		
		Intent intent=getIntent();
		keyNo=intent.getIntExtra("no", 0);
		keySymptom=intent.getStringExtra("key");
		http();
		
		
		
	}
	private void titlebar() {
		ImageView imageView1=(ImageView) findViewById(R.id.imageView1);
		
		imageView1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					finish();
			}
		});
		TextView textView1=(TextView) findViewById(R.id.textView1);
		textView1.setText("病症详情");
	}
	private void http() {
		RequestParams params=new RequestParams();
		params.addBodyParameter("no",String.valueOf(keyNo));
		HttpUtils http=new HttpUtils();
		http.send(
				HttpMethod.POST, 
				url, 
				params,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						Gson g=new Gson();
						data = g.fromJson(responseInfo.result, SickInfoDisease.class);
						setTvData();
						
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(SickInfoDiseaseDetailsActivity.this,
								"onErrorResponse" + error.getMessage(),
								Toast.LENGTH_LONG).show();
						
					}
				});
		
		
	}

	private void initTv() {
		tv_name=(TextView) findViewById(R.id.sifdisease_details_tv_name);
		tv_symptom=(TextView) findViewById(R.id.sifdisease_details_tv_symptom);
		tv_definition=(ExpandableTextView) findViewById(R.id.sifdisease_details_tv_definition);
		tv_cause=(ExpandableTextView) findViewById(R.id.sifdisease_details_tv_cause);
		tv_cure=(ExpandableTextView) findViewById(R.id.sifdisease_details_tv_cure);
	}
	private void setTvData() {
		//设置药品名
		tv_name.setText(data.getName());
		//设置症状
		String symptom=data.getSymptom();
		
		SpannableStringBuilder sp=SpanUtil.setColor(keySymptom,symptom,Color.RED);
		tv_symptom.setText(sp);
		
		tv_definition.setOriginalText(data.getDefinition());
		tv_definition.setText(data.getDefinition());
		tv_definition.setOnClickListener(new ExpandableTextViewOnclickListener(tv_definition));
		tv_cause.setOriginalText(data.getCause());
		tv_cause.setText(data.getCause());
		tv_cause.setOnClickListener(new ExpandableTextViewOnclickListener(tv_cause));
		
		String cure=data.getCure();
		String tcure=cure.replace('[', ' ').replace(']', ' ');
		SpannableStringBuilder sp_cure=new SpannableStringBuilder(tcure);
		int end=cure.lastIndexOf("]");
		if (end>0) {
			int index = 0;
			while (index!=end) {
				int start=cure.indexOf("[",index);
				int end2=cure.indexOf("]",start);
				index=end2;
				String key=cure.substring(start+1, end2);//得到药品名关键字
//				SpanUtil.setColors(key,cure,sp_cure,Color.BLUE);
				sp_cure.setSpan(new EtvClickable(key),start+1, end2,
						Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				System.out.println(key);
			}
			tv_cure.setOriginalText(sp_cure);
			tv_cure.setText(sp_cure);
			tv_cure.setOnClickListener(new ExpandableTextViewOnclickListener(tv_cure));
			tv_cure.setMovementMethod(LinkMovementMethod.getInstance());
		}
	}
	/**
	 * 完成自定义textview的点击收放文字事件
	 * @author Administrator
	 *
	 */
	private class ExpandableTextViewOnclickListener implements OnClickListener{
		private ExpandableTextView etv;
		public ExpandableTextViewOnclickListener(ExpandableTextView etv){
			this.etv=etv;
		}
		@Override
		public void onClick(View v) {
			etv.setTrim(!etv.getTrim());
			etv.setText();
			etv.requestFocusFromTouch();
			
		}
		
	}
	/**
	 *自定义textview中部分文字跳转事件
	 * @author Administrator
	 *
	 */
	private class EtvClickable extends ClickableSpan implements OnClickListener {
		private String key;

		public EtvClickable(String key) {
			super();
			this.key = key;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			// TODO Auto-generated method stub
			ds.setColor(Color.BLUE);
			ds.setUnderlineText(false);
		}

		@Override
		public void onClick(View widget) {
			Intent intent = new Intent(SickInfoDiseaseDetailsActivity.this, OtherActivity.class);
			intent.putExtra("key", key);
			startActivity(intent);

		}

	}
	
}
